'Copyright 2006 James Downey

Imports FetchXML
Imports FetchXMLBuilder.MetadataServiceSdk

Public Class frmOrder

    Private CurrentOrders As Orders
    Private NewOrders As Orders
    Private Entity As EntityMetadata

    Public Sub New(ByVal orders As Orders, ByVal Entity As EntityMetadata)
        InitializeComponent()
        Me.Entity = Entity
        Me.CurrentOrders = orders
        Me.NewOrders = New Orders()
        Dim order As Order
        Dim neworder As Order
        For Each order In orders
            neworder = New Order
            neworder.Attribute = order.Attribute
            neworder.Descending = order.Descending
            Me.NewOrders.Add(neworder)
        Next
    End Sub

    Public Sub LoadOrder()
        Me.lstOrder.Items.Clear()
        Dim order As Order
        For Each order In Me.NewOrders
            Me.lstOrder.Items.Add(order)
        Next
    End Sub

    Private Sub frmOrder_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load
        Dim i As Integer
        For i = 0 To entity.Attributes.Length - 1
            Me.lstAttributes.Items.Add(entity.Attributes(i).Name)
        Next
        LoadOrder()
    End Sub

    Private Sub btnAdd_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAdd.Click
        Dim attrib As String
        Dim order As Order
        Dim found As Boolean
        For Each attrib In Me.lstAttributes.SelectedItems
            found = False
            For Each order In Me.NewOrders
                If attrib = order.Attribute Then
                    found = True
                    Exit For
                End If
            Next
            If Not found Then
                NewOrders.Add(New Order(attrib))
            End If
        Next
        LoadOrder()
    End Sub

    Private Sub btnRemove_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnRemove.Click
        If Not Me.lstOrder.SelectedIndex = -1 Then
            Me.NewOrders.Remove(Me.lstOrder.SelectedItem)
        End If
        LoadOrder()
    End Sub

    Private Sub btnAscending_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnAscending.Click
        If Not Me.lstOrder.SelectedIndex = -1 Then
            CType(Me.lstOrder.SelectedItem, Order).Descending = False
        End If
        LoadOrder()
    End Sub

    Private Sub frmDescending_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles frmDescending.Click
        If Not Me.lstOrder.SelectedIndex = -1 Then
            CType(Me.lstOrder.SelectedItem, Order).Descending = True
        End If
        LoadOrder()
    End Sub

    Private Sub btnUp_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnUp.Click
        If Not Me.lstOrder.SelectedIndex = -1 Then
            Dim CurrentIndex As Integer
            CurrentIndex = Me.NewOrders.IndexOf(Me.lstOrder.SelectedItem)
            If Not CurrentIndex = 0 Then
                Me.NewOrders.Remove(Me.lstOrder.SelectedItem)
                Me.NewOrders.Insert(CurrentIndex - 1, Me.lstOrder.SelectedItem)
                LoadOrder()
                Me.lstOrder.SelectedItem = Me.lstOrder.Items.Item(CurrentIndex - 1)
            End If
        End If
    End Sub

    Private Sub btnCancel_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnCancel.Click
        Me.DialogResult = Windows.Forms.DialogResult.Cancel
        Me.Close()
    End Sub

    Private Sub btnOK_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnOK.Click
        Me.CurrentOrders.Clear()
        Dim order As Order
        For Each order In Me.NewOrders
            Me.CurrentOrders.Add(order)
        Next
        Me.DialogResult = Windows.Forms.DialogResult.OK
        Me.Close()
    End Sub

End Class